<template>
  <div>
    <el-dialog title="新建项" :visible.sync="dialogVisible" @close="close">
      <el-form :model="formData" :rules="rules" ref="form" label-width="120px">
        <el-form-item label="名称" prop="name" label-width="120px">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addBaseconst } from "@/api/dictionary";
export default {
  data() {
    return {
      formData: {
        name: "",
        baseTypeId: 0
      },
      dialogVisible: false,
      rules: {
        name: [{ required: true, message: "请输入", trigger: "blur" }]
      }
    };
  },
  methods: {
    close() {
      this.dialogVisible=false;
    },
    show(baseTypeId) {
      this.formData.baseTypeId = baseTypeId;
      this.dialogVisible = true;
    },
    save() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          let r = await addBaseconst(this.formData);
          if (r && r.error == 0) {
            this.$refs["form"].resetFields();
            this.$emit("finish", this.formData.baseTypeId);
            this.close();
          } else {
            this.$message({
              message: "出错了",
              type: "error"
            });
          }
        } else {
          return false;
        }
      });
    }
  }
};
</script>
